using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Volo.Abp;

namespace Matrix.Configuration
{
    public class Program
    {
        public static async Task<int> Main(string[] args)
        {
            // 配置 PostgreSQL DateTime 时区处理
            AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

            var builder = WebApplication.CreateBuilder(args);

            builder.Host.UseAutofac()
                   .UseSerilog((_, configuration) =>
                   {
                       configuration.MinimumLevel.Information();
                       configuration.WriteTo.Console();
                       configuration.WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day);
                   });

            await builder.AddApplicationAsync<MatrixConfigurationHttpApiHostModule>();

            var app = builder.Build();

            await app.InitializeApplicationAsync();

            // 确保数据库被创建
            using var scope = app.Services.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<MatrixConfigurationDbContext>();
            await dbContext.Database.EnsureCreatedAsync();

            await app.RunAsync();

            return 0;
        }
    }
}